/*
 * (C) Copyright 2014-2016, by Dimitrios Michail
 *
 * JHeaps Library
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jheaps.tree

import junit.framework.TestCase.assertEquals
import org.jheaps.DoubleEndedAddressableHeap
import org.jheaps.DoubleEndedAddressableHeap.DoubleEndedHandle
import org.junit.Assert
import org.junit.Test
import java.util.*

abstract class AbstractDoubleEndedAddressableHeapTest : AbstractAddressableHeapTest() {

    abstract override fun createHeap(): DoubleEndedAddressableHeap<Int, Void>

    abstract override fun createHeap(comparator: Comparator<Int?>?): DoubleEndedAddressableHeap<Int, Void>

    abstract override fun createHeapWithStringValues(): DoubleEndedAddressableHeap<Int, String>

    @Test
    override fun test() {
        val h = createHeap()

        for (i in 0 until SIZE) {
            h.insert(i)
            assertEquals(0, h.findMin().key)
            assertEquals(i, h.findMax().key)
            Assert.assertFalse(h.isEmpty)
            Assert.assertEquals(h.size(), (i + 1).toLong())
        }

        for (i in SIZE - 1 downTo 0) {
            assertEquals(h.findMin().key, SIZE - i - 1)
            assertEquals(h.findMax().key, SIZE - 1)
            h.deleteMin()
        }
    }

    @Test
    override fun testOnlyInsert() {
        val h = createHeap()

        for (i in 0 until SIZE) {
            h.insert(SIZE - i)
            assertEquals(SIZE - i, h.findMin().key)
            assertEquals(SIZE, h.findMax().key)
            Assert.assertFalse(h.isEmpty)
            Assert.assertEquals(h.size(), (i + 1).toLong())
        }
    }

    @Test
    override fun testComparator() {
        val h = createHeap(comparator)

        var i = 0
        while (i < SIZE) {
            h.insert(i)
            assertEquals(i, h.findMin().key)
            assertEquals(0, h.findMax()!!.key)
            Assert.assertFalse(h.isEmpty)
            Assert.assertEquals(h.size(), (i + 1).toLong())
            i++
        }

        i = SIZE - 1
        while (i >= 0) {
            assertEquals(h.findMin().key, i)
            assertEquals(h.findMax()!!.key, 0)
            h.deleteMin()
            i--
        }
    }

    @Test
    override fun testOnly4() {
        val h = createHeap()

        Assert.assertTrue(h.isEmpty)

        h.insert(780)
        Assert.assertEquals(h.size(), 1)
        assertEquals(780, h.findMin().key)
        assertEquals(780, h.findMax()!!.key)

        h.insert(-389)
        Assert.assertEquals(h.size(), 2)
        assertEquals(-389, h.findMin().key)
        assertEquals(780, h.findMax()!!.key)

        h.insert(306)
        Assert.assertEquals(h.size(), 3)
        assertEquals(-389, h.findMin().key)
        assertEquals(780, h.findMax()!!.key)

        h.insert(579)
        Assert.assertEquals(h.size(), 4)
        assertEquals(-389, h.findMin().key)
        assertEquals(780, h.findMax()!!.key)

        h.deleteMin()
        Assert.assertEquals(h.size(), 3)
        assertEquals(306, h.findMin().key)
        assertEquals(780, h.findMax()!!.key)

        h.deleteMax()
        Assert.assertEquals(h.size(), 2)
        assertEquals(306, h.findMin().key)
        assertEquals(579, h.findMax()!!.key)

        h.deleteMin()
        Assert.assertEquals(h.size(), 1)
        assertEquals(579, h.findMin().key)
        assertEquals(579, h.findMax()!!.key)

        h.deleteMax()
        Assert.assertEquals(h.size(), 0)

        Assert.assertTrue(h.isEmpty)
    }

    @Test
    override fun testOnly4Reverse() {
        val h = createHeap(comparator)

        Assert.assertTrue(h.isEmpty)

        h.insert(780)
        Assert.assertEquals(h.size(), 1)
        assertEquals(780, h.findMin().key)
        assertEquals(780, h.findMax()!!.key)

        h.insert(-389)
        Assert.assertEquals(h.size(), 2)
        assertEquals(780, h.findMin().key)
        assertEquals(-389, h.findMax()!!.key)

        h.insert(306)
        Assert.assertEquals(h.size(), 3)
        assertEquals(780, h.findMin().key)
        assertEquals(-389, h.findMax()!!.key)

        h.insert(579)
        Assert.assertEquals(h.size(), 4)
        assertEquals(780, h.findMin().key)
        assertEquals(-389, h.findMax()!!.key)

        h.deleteMin()
        Assert.assertEquals(h.size(), 3)
        assertEquals(579, h.findMin().key)
        assertEquals(-389, h.findMax()!!.key)

        h.deleteMax()
        Assert.assertEquals(h.size(), 2)
        assertEquals(579, h.findMin().key)
        assertEquals(306, h.findMax()!!.key)

        h.deleteMin()
        Assert.assertEquals(h.size(), 1)
        assertEquals(306, h.findMin().key)
        assertEquals(306, h.findMax()!!.key)

        h.deleteMin()
        Assert.assertEquals(h.size(), 0)

        Assert.assertTrue(h.isEmpty)
    }

    @Test
    fun testSortRandomSeed1Max() {
        val h = createHeap()

        val generator = Random(1)

        for (i in 0 until SIZE) {
            h.insert(generator.nextInt())
        }

        var prev: Int? = null
        var cur: Int?
        while (!h.isEmpty) {
            cur = h.findMax()!!.key
            h.deleteMax()
            if (prev != null) {
                Assert.assertTrue(prev.compareTo(cur!!) >= 0)
            }
            prev = cur
        }
    }

    @Test
    fun testSort1RandomSeed1Max() {
        val h = createHeap()

        val generator = Random(1)

        for (i in 0 until SIZE) {
            h.insert(generator.nextInt())
        }

        var prev: Int? = null
        var cur: Int?
        while (!h.isEmpty) {
            cur = h.deleteMax()!!.key
            if (prev != null) {
                Assert.assertTrue(prev.compareTo(cur!!) >= 0)
            }
            prev = cur
        }
    }

    @Test
    fun testSortRandomSeed2Max() {
        val h = createHeap()

        val generator = Random(2)

        for (i in 0 until SIZE) {
            h.insert(generator.nextInt())
        }

        var prev: Int? = null
        var cur: Int?
        while (!h.isEmpty) {
            cur = h.findMax()!!.key
            h.deleteMax()
            if (prev != null) {
                Assert.assertTrue(prev.compareTo(cur!!) >= 0)
            }
            prev = cur
        }
    }

    @Test
    fun testSort2RandomSeed2Max() {
        val h = createHeap()

        val generator = Random(2)

        for (i in 0 until SIZE) {
            h.insert(generator.nextInt())
        }

        var prev: Int? = null
        var cur: Int?
        while (!h.isEmpty) {
            cur = h.deleteMax()!!.key
            if (prev != null) {
                Assert.assertTrue(prev.compareTo(cur!!) >= 0)
            }
            prev = cur
        }
    }

    @Test
    fun testFindMaxDeleteMaxSameObject() {
        val h = createHeap()

        val generator = Random(1)

        for (i in 0 until SIZE) {
            h.insert(generator.nextInt())
        }

        while (!h.isEmpty) {
            Assert.assertEquals(h.findMax(), h.deleteMax())
        }
    }

    @Test
    override fun testDelete() {
        val h = createHeap()

        val array: Array<DoubleEndedHandle<Int, Void>> = Array(15){h.insert(it)}
        /*array = arrayOfNulls<DoubleEndedHandle<*, *>>(15)
        for (i in 0..14) {
            array[i] = h.insert(i)
        }*/

        array[5].delete()
        assertEquals(0, h.findMin().key)
        assertEquals(14, h.findMax()!!.key)
        array[7].delete()
        assertEquals(0, h.findMin().key)
        assertEquals(14, h.findMax()!!.key)
        array[0].delete()
        assertEquals(1, h.findMin().key)
        assertEquals(14, h.findMax()!!.key)
        array[2].delete()
        assertEquals(1, h.findMin().key)
        assertEquals(14, h.findMax()!!.key)
        array[1].delete()
        assertEquals(3, h.findMin().key)
        assertEquals(14, h.findMax()!!.key)
        array[3].delete()
        assertEquals(4, h.findMin().key)
        assertEquals(14, h.findMax()!!.key)
        array[9].delete()
        assertEquals(4, h.findMin().key)
        assertEquals(14, h.findMax()!!.key)
        array[4].delete()
        assertEquals(6, h.findMin().key)
        assertEquals(14, h.findMax()!!.key)
        array[8].delete()
        assertEquals(6, h.findMin().key)
        assertEquals(14, h.findMax()!!.key)
        array[11].delete()
        assertEquals(6, h.findMin().key)
        assertEquals(14, h.findMax()!!.key)
        array[6].delete()
        assertEquals(10, h.findMin().key)
        assertEquals(14, h.findMax()!!.key)
        array[12].delete()
        assertEquals(10, h.findMin().key)
        assertEquals(14, h.findMax()!!.key)
        array[10].delete()
        assertEquals(13, h.findMin().key)
        assertEquals(14, h.findMax()!!.key)
        array[13].delete()
        assertEquals(14, h.findMin().key)
        assertEquals(14, h.findMax()!!.key)
        array[14].delete()
        Assert.assertTrue(h.isEmpty)
    }

    @Test
    override fun testDelete1() {
        val h = createHeap()

        val array: Array<DoubleEndedHandle<Int, Void>> = Array(8){h.insert(it)}
        /*array = arrayOfNulls<DoubleEndedHandle<*, *>>(8)
        for (i in 0..7) {
            array[i] = h.insert(i)
        }*/

        array[5].delete()
        assertEquals(0, h.findMin().key)
        assertEquals(7, h.findMax()!!.key)
        array[7].delete()
        assertEquals(0, h.findMin().key)
        assertEquals(6, h.findMax()!!.key)
        array[0].delete()
        assertEquals(1, h.findMin().key)
        assertEquals(6, h.findMax()!!.key)
        array[2].delete()
        assertEquals(1, h.findMin().key)
        assertEquals(6, h.findMax()!!.key)
        array[1].delete()
        assertEquals(3, h.findMin().key)
        assertEquals(6, h.findMax()!!.key)
    }

    @Test
    override fun testAddDelete() {
        val h = createHeap()

        val array: Array<DoubleEndedHandle<Int, Void>> = Array(SIZE){h.insert(it)}
       /* array = arrayOfNulls<DoubleEndedHandle<*, *>>(SIZE)
        for (i in 0 until SIZE) {
            array[i] = h.insert(i)
        }*/

        for (i in SIZE - 1 downTo 0) {
            array[i].delete()
            if (i > 0) {
                assertEquals(0, h.findMin().key)
                assertEquals(i - 1, h.findMax()!!.key)
            }
        }
        Assert.assertTrue(h.isEmpty)
    }

    @Test
    override fun testAddDeleteComparator() {
        val h = createHeap(comparator)

        val array: Array<DoubleEndedHandle<Int, Void>> = Array(SIZE){h.insert(it)}
        /*array = arrayOfNulls<DoubleEndedHandle<*, *>>(SIZE)
        for (i in 0 until SIZE) {
            array[i] = h.insert(i)
        }*/

        for (i in 0 until SIZE) {
            array[i].delete()
            if (i < SIZE - 1) {
                assertEquals(SIZE - 1, h.findMin().key)
                assertEquals(i + 1, h.findMax()!!.key)
            }
        }
        Assert.assertTrue(h.isEmpty)
    }

    @Test
    override fun testAddDecreaseKeyDeleteMin() {
        val h = createHeap()

        val array: Array<DoubleEndedHandle<Int, Void>> = Array(SIZE){h.insert(it)}
        /*array = arrayOfNulls<DoubleEndedHandle<*, *>>(SIZE)

        for (i in 0 until SIZE) {
            array[i] = h.insert(i)
        }*/

        assertEquals(0, h.findMin().key)
        assertEquals(SIZE - 1, h.findMax()!!.key)

        for (i in SIZE / 2 until SIZE / 2 + 10) {
            array[i].decreaseKey(i / 2)
        }

        array[0].delete()

        assertEquals(1, h.findMin().key)

        for (i in SIZE / 2 + 10 until SIZE / 2 + 20) {
            array[i].decreaseKey(0)
        }

        assertEquals(0, h.deleteMin().key)

        array[SIZE - 1].delete()

        assertEquals(SIZE - 2, h.findMax()!!.key)

        for (i in SIZE / 2 + 20 until SIZE / 2 + 30) {
            array[i].increaseKey(SIZE - 1)
        }

        assertEquals(SIZE - 1, h.deleteMax()!!.key)
    }

    @Test
    override fun testAddDecreaseKeyDeleteMinComparator() {
        val h = createHeap(comparator)

        val array: Array<DoubleEndedHandle<Int, Void>> = Array(SIZE){h.insert(it)}
       /* array = arrayOfNulls<DoubleEndedHandle<*, *>>(SIZE)
        for (i in 0 until SIZE) {
            array[i] = h.insert(i)
        }*/

        assertEquals(SIZE - 1, h.findMin().key)
        assertEquals(0, h.findMax()!!.key)

        for (i in SIZE / 2 until SIZE / 2 + 10) {
            array[i].decreaseKey(SIZE - 1)
        }

        array[SIZE - 1].delete()

        for (i in SIZE / 2 + 10 until SIZE / 2 + 20) {
            array[i].decreaseKey(SIZE - 1)
        }

        assertEquals(SIZE - 1, h.deleteMin().key)

        assertEquals(0, h.findMax()!!.key)
        array[0].delete()
        assertEquals(1, h.findMax()!!.key)

        for (i in SIZE / 2 + 20 until SIZE / 2 + 30) {
            array[i].increaseKey(0)
        }

        assertEquals(0, h.findMax()!!.key)
    }

    @Test(expected = IllegalArgumentException::class)
    fun testDeleteMaxDeleteTwice() {
        val h = createHeap()
        h.insert(50)
        val e1 = h.insert(100)
        h.deleteMax()
        e1.delete()
    }

    @Test(expected = IllegalArgumentException::class)
    fun testDeleteMaxDeleteTwice1() {
        val h = createHeap()

        for (i in 100..199) {
            h.insert(i)
        }

        h.deleteMax()!!.delete()
    }

    @Test(expected = IllegalArgumentException::class)
    fun testDeleteMaxDecreaseKey() {
        val h = createHeap()

        for (i in 100..199) {
            h.insert(i)
        }
        h.deleteMax()!!.decreaseKey(0)
    }

    @Test(expected = IllegalArgumentException::class)
    fun testDeleteMaxIncreaseKey() {
        val h = createHeap()

        for (i in 100..199) {
            h.insert(i)
        }
        h.deleteMax()!!.increaseKey(200)
    }

    @Test(expected = NoSuchElementException::class)
    fun testNoElementFindMax() {
        val h = createHeap()
        h.findMax()
    }

    @Test(expected = NoSuchElementException::class)
    fun testNoElementDeleteMax() {
        val h = createHeap()
        h.deleteMax()
    }

    @Test(expected = IllegalArgumentException::class)
    fun testWrongIncreaseKey() {
        val h = createHeap()
        h.insert(15).increaseKey(14)
    }

    @Test
    fun testSameKeyAfterIncreaseDecrease() {
        val h = createHeap()

        Assert.assertTrue(h.isEmpty)

        val doubleEndedHandle = h.insert(780)
        doubleEndedHandle.increaseKey(780)
        assertEquals(780, h.deleteMax()!!.key)

        h.insert(500)
        h.insert(600)
        val doubleEndedHandle1 = h.insert(700)
        doubleEndedHandle1.increaseKey(700)
        assertEquals(700, doubleEndedHandle1.key)
        doubleEndedHandle1.decreaseKey(700)
        assertEquals(700, doubleEndedHandle1.key)
    }
}
